<template>
  <!--确认价格-->
  <el-dialog
    :close-on-click-modal="false"
    title="确认价格"
    :visible.sync="dialogVisible"
    :beforeClose="handleClose"
    width="400px"
  >
    <div>
      <el-form
        size="mini"
        :model="sendForm"
        label-width="100px"
        ref="sendForm"
        :rules="rules"
      >
        <el-form-item label="销售还价："
          >{{ sendForm.reducePrice }} 元</el-form-item
        >
        <el-form-item label="确认价格：" prop="purchasePrice">
          <el-input
            style="width: 100px"
            type="number"
            clearable
            v-model="sendForm.purchasePrice"
          ></el-input
          >元
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleConfirm" :loading="btnLoading"
        >确认价格</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { inquiryOperate } from "@/api/allInLine/inquiry";
import { mapGetters } from "vuex";
import { Message } from "element-ui";
const defaultSendForm = {
  priceId: "",
  inquiryId: "", //询价记录ID
  operateType: "priceSure", //操作类型(询运费：inquiryFreight,还价：inquiryReduce,未成交：unOrder,取消询价：cancel,确认价格：priceSure)
  reducePrice: "", //还价的价格(还价操作需填)
  purchasePrice: "", //采购底价(确认价格操作需填)
  unOrderReason: "",
};

export default {
  name: "index",
  data() {
    const validatePurchasePrice = (rule, value, callback) => {
      if (value < 0) {
        callback(new Error('价格不能小于0元'))
      }
      callback()
    };
    return {
      dialogVisible: false,
      btnLoading: false,
      sendForm: JSON.parse(JSON.stringify(defaultSendForm)),
      rules: {
        purchasePrice: [
          { required: true, message: "确认价格必填", trigger: "blur" },
          { validator:validatePurchasePrice,trigger: "blur"}
        ],
      },
      options: [],
    };
  },
  computed: {
    ...mapGetters(["name", "mobile", "userId"]),
  },
  components: {},
  created() {},
  filters: {},
  methods: {
    //显示还价
    showPop(item) {
      this.dialogVisible = true;
      this.sendForm.inquiryId = item.inquiryId;
      this.sendForm.reducePrice = item.reducePrice;
      this.sendForm.purchasePrice = item.purchasePrice;
      this.sendForm.priceId = item.priceId;
    },
    //关闭还价
    handleClose() {
      this.dialogVisible = false;
      this.$refs.sendForm.resetFields();
      this.sendForm = JSON.parse(JSON.stringify(defaultSendForm));
    },

    //确认还价
    handleConfirm() {
      this.$refs.sendForm.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          const data = JSON.parse(JSON.stringify(this.sendForm));
          inquiryOperate(data)
            .then((res) => {
              Message.success("确认成功");
              this.btnLoading = false;
              this.$refs.sendForm.resetFields();
              this.sendForm = JSON.parse(JSON.stringify(defaultSendForm));
              this.$emit("update");
              this.dialogVisible = false;
            })
            .catch((error) => {
              this.btnLoading = false;
            });
        }
      });
    },
  },
};
</script>

<style  lang="scss">
</style>
